package com.chee.plant.server;

import java.util.Enumeration;
import java.util.Hashtable;

import org.apache.mina.common.IoSession;
import org.json.JSONException;
import org.json.JSONObject;

import com.chee.plant.logic.PlantCmd;

/**
 * 通信消息对象
 * 
 * @author lover 消息格式: {"userId":xx, "command":xx, "result":xx,
 *         "data":{.......}}
 */
public class Message {
	private static String Str_UserId = "userId";
	private static String Str_Command = "command";
	private static String Str_Result = "result";
	private static String Str_Data = "data";
	/**
	 * 该消息要发送到的session
	 */
	private IoSession _session;
	/**
	 * 该消息要发送到的用户ID, 如果需要广播消息, 那么设置为0
	 */
	private int _userId;
	/**
	 * 消息包含的命令
	 */
	private PlantCmd _command;
	/**
	 * 消息的结果
	 */
	private int _result;
	/**
	 * 消息所包含的消息段, 可以包含若干个消息段,
	 */
	private Hashtable<String, Object> _vars;
	private String _content;

	public Message() {
		this._vars = new Hashtable<String, Object>();
	}

	public Message(String content) {
		this._vars = new Hashtable<String, Object>();
		this.setContent(content);
	}

	/**
	 * 构造消息实例
	 * 
	 * @param userId
	 *            - 用户ID
	 * @param command
	 *            - 消息命令
	 * @param result
	 *            - 消息结果
	 */
	public Message(int userId, PlantCmd command, int result) {
		this._userId = userId;
		this._command = command;
		this._result = result;
		this._vars = new Hashtable<String, Object>();
	}

	public void setSession(IoSession session) {
		this._session = session;
	}

	public IoSession getSession() {
		return this._session;
	}

	public void setUserId(int userId) {
		this._userId = userId;
	}

	public int getUserId() {
		return this._userId;
	}

	public String getContent() {
		this.encryptContents();
		return this._content;
	}

	public void setCommand(PlantCmd command) {
		this._command = command;
	}

	public PlantCmd getCommand() {
		return this._command;
	}

	public void setResult(int result) {
		this._result = result;
	}

	public int getResult() {
		return this._result;
	}

	public Hashtable<String, Object> getVariables() {
		if (this._vars.size() == 0)
			return new Hashtable<String, Object>();
		return this._vars;
	}

	public Object getData(String key) {
		if (this._vars.containsKey(key))
			return this._vars.get(key);
		else
			return null;
	}

	public void addVariable(String key, Object value) {
		this._vars.put(key, value);
	}

	public void removeVariable(String key) {
		this._vars.remove(key);
	}

	private void encryptContents() {
		JSONObject json = new JSONObject();
		try {
			json.put(Str_UserId, this.getUserId());
			json.put(Str_Command, this.getCommand().ordinal());
			json.put(Str_Result, this.getResult());
			JSONObject data = new JSONObject();
			Enumeration<String> enu = this._vars.keys();
			while (enu.hasMoreElements()) {
				String ele = enu.nextElement();
				data.put(ele, this._vars.get(ele));
			}
			json.put(Str_Data, data);

		} catch (JSONException ex) {
			// TODO: json exception
		}
		String jsonStr = json.toString();
		this._content = CommonData.getDes().encryptString(jsonStr);
	}

	public boolean setContent(String encrypted) {
		String content = CommonData.getDes().decryptString(encrypted);
		this._content = content;
		try {
			JSONObject json = new JSONObject(content);
			this._userId = json.getInt(Str_UserId);
			this._command = PlantCmd.getCmd(json.getInt(Str_Command));
			this._result = json.getInt(Str_Result);
			JSONObject data = json.getJSONObject(Str_Data);
			String[] keys = JSONObject.getNames(data);
			for (int i = 0; i < keys.length; i++) {
				this.addVariable(keys[i], data.get(keys[i]));
			}
		} catch (JSONException e) {
			return false;
		}
		return true;
	}

	public static void main(String[] args) {

	}
}
